package ThreadPool.lagou.concurrent.completableFuture.demo01;

import java.util.concurrent.*;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: CompletableFuture测试
 * @date 2022/10/6 21:37
 */
public class CompletableFutureBuildDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 1.异步无返回值
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(()->{
            System.out.println(Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },executorService);
        // 2.异步有返回值
        CompletableFuture<String> objectCompletableFuture = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "有返回值的异步处理方法";
        },executorService);
        System.out.println(objectCompletableFuture.get());
        System.out.println(112312);
        executorService.shutdown();
    }
}
